package com.suyang.syt.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.suyang.syt.dao.interfaces.SystemNavigationDao;
import com.suyang.syt.model.SystemNavigation;
import com.suyang.syt.model.dto.AjaxObj;
import com.suyang.syt.model.dto.SystemNavigationDTO;
import com.suyang.syt.service.interfaces.SystemNavigationService;
import com.suyang.syt.util.EmptyUtil;

@Service
public class SystemNavigationServiceImpl implements SystemNavigationService {
	@Autowired
	private SystemNavigationDao systemNavigationDao;

	@Override
	public List<SystemNavigationDTO> listTreegrid(long id) throws Exception {
		List<SystemNavigation> list = systemNavigationDao.list(" from SystemNavigation s where s.pid=? order by s.orderIndex ", id);
		List<SystemNavigationDTO> temp = new ArrayList<SystemNavigationDTO>();
		for (SystemNavigation systemNavigation : list) {
			temp.add(new SystemNavigationDTO(systemNavigation));
		}
		return temp;
	}

	@Override
	public List<SystemNavigationDTO> list(String flag) throws Exception {
		List<SystemNavigationDTO> temp = new ArrayList<SystemNavigationDTO>();
		StringBuilder hql = new StringBuilder(" from SystemNavigation s where s.pid ");
		if ("listNavAdmin".equals(flag)) {
			hql.append("=0 order by orderIndex ");
		} else if ("listNavPXJG".equals(flag)) {
			hql.append("=3 order by orderIndex ");
		} else if ("listNavJGJS".equals(flag)) {
			hql.append("=4 order by orderIndex ");
		}
		List<SystemNavigation> list = systemNavigationDao.list(hql.toString());
		if (EmptyUtil.isNotEmpty(list)) {
			for (SystemNavigation systemNavigation : list) {
				temp.add(listChildren(systemNavigation));
			}
		}
		return temp;
	}

	/** 递归调用得到所有子节点 */
	private SystemNavigationDTO listChildren(SystemNavigation systemNavigation) {
		SystemNavigationDTO sysTreeDTO = new SystemNavigationDTO(systemNavigation);
		List<SystemNavigation> children = systemNavigationDao.list(" from SystemNavigation s where s.pid=? order by orderIndex ", systemNavigation.getId());
		List<SystemNavigationDTO> temp = new ArrayList<SystemNavigationDTO>();
		for (SystemNavigation navigation : children) {
			temp.add(listChildren(navigation));
		}
		sysTreeDTO.setChildren(temp);
		return sysTreeDTO;
	}

	@Override
	public AjaxObj add(SystemNavigationDTO systemNavigationDTO) throws Exception {
		if (systemNavigationDTO != null) {
			if (EmptyUtil.isEmpty(systemNavigationDTO.getText())) {
				return new AjaxObj(0, "菜单项名称不能为空！");
			}
			if (systemNavigationDao.theExistenceName(systemNavigationDTO.getText())) {
				return new AjaxObj(0, "已经存在该菜单项，请更换名称！");
			}
			if (EmptyUtil.isEmpty(systemNavigationDTO.getIconCls())) {
				return new AjaxObj(0, "图标不能为空！");
			}
			long pid = systemNavigationDTO.getPid();
			if (pid < 1) {
				return new AjaxObj(0, "上级菜单不能为空！");
			}
			SystemNavigation parent = systemNavigationDao.load(pid);
			if (parent == null) {
				return new AjaxObj(0, "读取导航菜单错误，请联系管理员！");
			}
			if ("open".equals(parent.getState())) {
				parent.setState("closed");
				systemNavigationDao.update(parent);
			}
			SystemNavigation systemNavigation = new SystemNavigation();
			systemNavigation.setPid(parent.getId());
			systemNavigation.setPtext(parent.getText());
			systemNavigation.setOrderIndex(systemNavigationDTO.getOrderIndex());
			systemNavigation.setUrl(systemNavigationDTO.getUrl());
			systemNavigation.setState("open");
			systemNavigation.setChecked("false");
			systemNavigation.setText(systemNavigationDTO.getText());
			systemNavigation.setIconCls(systemNavigationDTO.getIconCls());
			systemNavigationDao.add(systemNavigation);
			return new AjaxObj(1, "导航菜单添加成功！", systemNavigation);
		}
		return new AjaxObj(0, "导航菜单添加失败！");
	}

	@Override
	public AjaxObj delete(String ids) throws Exception {
		if (EmptyUtil.isNotEmpty(ids)) {
			String[] idss = ids.split(",");
			if (idss != null && idss.length > 0) {
				for (String id : idss) {
					if ("1".equals(id)) {
						return new AjaxObj(0, "不能删除系统导航根节点！");
					}
				}
				for (String id : idss) {
					delete(systemNavigationDao.load(Long.parseLong(id)));
				}
				return new AjaxObj(1, "删除成功！");
			}
		} else {
			return new AjaxObj(0, "请选择要删除的记录！");
		}
		return new AjaxObj(0, "删除失败！");
	}

	private void delete(SystemNavigation systemNavigation) {
		if (systemNavigation != null) {
			List<SystemNavigation> list = systemNavigationDao.list(" from SystemNavigation s where s.pid=? ", systemNavigation.getId());
			if (EmptyUtil.isNotEmpty(list)) {
				for (SystemNavigation sysNav : list) {
					delete(sysNav);
				}
			}
			systemNavigationDao.delete(systemNavigation);
		}
	}

	@Override
	public AjaxObj update(SystemNavigationDTO systemNavigationDTO) throws Exception {
		if (systemNavigationDTO != null) {
			if (systemNavigationDTO.getId() == 1) {
				return new AjaxObj(0, "不能更新系统导航根节点！");
			}
			if (systemNavigationDTO.getId() < 1) {
				return new AjaxObj(0, "菜单ID不能为空！");
			}
			SystemNavigation systemNavigation = systemNavigationDao.load(systemNavigationDTO.getId());
			if (systemNavigation == null) {
				return new AjaxObj(0, "读取菜单错误！");
			}
			if (EmptyUtil.isEmpty(systemNavigationDTO.getIconCls())) {
				return new AjaxObj(0, "图标不能为空！");
			}
			if (EmptyUtil.isEmpty(systemNavigationDTO.getText())) {
				return new AjaxObj(0, "菜单项名称不能为空！");
			}
			if (!systemNavigationDTO.getText().equals(systemNavigation.getText()) && systemNavigationDao.theExistenceName(systemNavigationDTO.getText())) {
				return new AjaxObj(0, "已经存在该菜单项，请更换名称！");
			}
			long pid = systemNavigationDTO.getPid();
			if (systemNavigationDTO.getPid() < 1) {
				return new AjaxObj(0, "上级菜单加载错误,请联系管理员！");
			}
			SystemNavigation parent = systemNavigationDao.load(pid);
			if (parent == null) {
				return new AjaxObj(0, "上级菜单加载错误,请联系管理员！");
			}
			if ("open".equals(parent.getState())) {
				parent.setState("closed");
				systemNavigationDao.update(parent);
			}
			systemNavigation.setPid(parent.getId());
			systemNavigation.setPtext(parent.getText());
			systemNavigation.setChecked("false");
			systemNavigation.setOrderIndex(systemNavigationDTO.getOrderIndex());
			systemNavigation.setText(systemNavigationDTO.getText());
			systemNavigation.setUrl(systemNavigationDTO.getUrl());
			systemNavigation.setIconCls(systemNavigationDTO.getIconCls());
			systemNavigationDao.update(systemNavigation);
			return new AjaxObj(1, "导航菜单更新成功！", systemNavigation);
		}
		return new AjaxObj(0, "导航菜单更新失败！");
	}

}